<template>
  <div class="count-box">
    <button class="minus" @click="handMinus">-</button>
    <input type="text" class="inp" :value="num" @change="handChange">
    <button class="add" @click="handAdd">+</button>
  </div>
</template>

<script>
export default {
  props: {
    num: {
      type: Number,
      default: 1
    }
  },
  methods: {
    // 减
    handMinus () {
      if (this.num <= 1) return
      this.$emit('input', this.num - 1)
    },
    // 加
    handAdd () {
      this.$emit('input', this.num + 1)
    },
    // 输入框的值的变化
    handChange (e) {
      // 获取输入框的值
      let value = e.target.value
      // 进行判断value是否合法
      if (value <= 0 || isNaN(value)) {
        value = 1
      }
      this.$emit('input', value)
    }
  }

}
</script>

<style lang="less" scoped>
.count-box{
  width: 110px;
  display: flex;
  .add ,.minus{
    width: 30px;
    height: 30px;
    outline: none;
    border: none;
    background-color: #efefef;
  }
  .inp{
    margin: 0 5px;
    width: 40px;
    height: 30px;
    outline: none;
    border: none;
    text-align: center;
    background-color: #efefef;
  }
}

</style>
